<template>
  <div class="img_upload">
    <el-upload
      ref="upload"
      class="avatar-uploader"
      action="#"
      :auto-upload="true"
      list-type="picture-card"
      :file-list="fileList | filterFile"
      :before-upload="handleBeforeUpload"
      :on-remove="remove"
      :on-change="result"
      :on-preview="handlePictureCardPreview"
      :http-request="uploadFile"
      :disabled="disabled"
      :on-exceed="handleExceed"
      :drag="drag"
      accept="image/png, image/jpeg, image/jpg"
      multiple
      :class="{ hide: (this.fileList && this.fileList.length) >= this.limit }"
      :limit="limit"
    >
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog append-to-body :visible.sync="dialogVisible">
      <i class="el-icon-download img_upload_download_index" @click="clickDwo">
      </i>
      <img width="100%" :src="dialogImageUrl" />
    </el-dialog>
  </div>
</template>

<script>
import configS from "@/utils/config";
export default {
  filters: {
    // 判断是否有 "http://"
    filterFile(val) {
      if (val) {
        return val.map((item) => {
          if (item) {
            let url = item.url || item.previewPath || item.path || item.filePath;
            if (url.indexOf("http://") !== -1 || url.indexOf("https://") !== -1) {
              return {
                ...item,
              };
            } else {
              return {
                ...item,
                url: configS.imgUrl + url,
              };
            }
          } else {
            return {
              ...item,
            };
          }
        });
      } else {
        return [];
      }
    },
  },
  props: {
    fileList: {
      type: Array,
      default: function () {
        return [];
      },
    },
    fileType: {
      type: Array,
      default: function () {
        return ["png", "jpg", "jpeg"];
      },
    },
    // 大小限制(MB)
    fileSize: {
      type: Number,
      default: 100,
    },
    // 图片数量限制
    limit: {
      type: Number,
      default: 100,
    },
    // 图片数量限制
    drag: {
      type: Boolean,
      default: false,
    },
    disabled: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      dialogVisible: false,
      dialogImageName: "",
      dialogImageUrl: "",
    };
  },
  methods: {
    clickDwo() {
      if (this.dialogImageUrl) {
        this.$download.download(this.dialogImageUrl, this.dialogImageName);
      } else {
        this.$modal.msgWarning("请上传成功后再下载图片");
      }
    },
    handlePictureCardPreview(file) {
      this.dialogImageName = file.name || file.fileName || file.originalName;
      this.dialogImageUrl = file.url || file.path || file.filePath || file.previewPath;
      this.dialogVisible = true;
    },
    result(file, all) {
      this.$emit("change", [file, all]);
      this.$emit("update:fileList", all);
    },
    remove(file, all) {
      this.$emit("remove", [file, all]);
      this.$emit("update:fileList", all);
    },
    // 绑定在http-request,覆盖上传事件
    uploadFile(file) {
      this.$emit("upload", file);
    },
    handleExceed() {
      this.$message.error(`上传文件数量不能超过 ${this.limit} 个!`);
    },
    // 上传前loading加载
    handleBeforeUpload(file) {
      let isImg = false;
      if (this.fileType.length) {
        let fileExtension = "";
        if (file.name.lastIndexOf(".") > -1) {
          fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
        }
        isImg = this.fileType.some((type) => {
          if (file.type.indexOf(type) > -1) return true;
          if (fileExtension && fileExtension.indexOf(type) > -1) return true;
          return false;
        });
      } else {
        isImg = file.type.indexOf("image") > -1;
      }

      if (!isImg) {
        this.$message.error(
          `文件格式不正确, 请上传${this.fileType.join("/")}图片格式文件!`
        );
        return false;
      }
      if (this.fileSize) {
        const isLt = file.size / 1024 / 1024 < this.fileSize;
        if (!isLt) {
          this.$message.error(`上传头像图片大小不能超过 ${this.fileSize} MB!`);
          return false;
        }
      }
      return true;
    },
  },
};
</script>
<style lang="scss">
.img_upload {
  .el-upload-list__item.is-ready {
    float: left;
    width: 90px !important;
    height: 90px !important;
  }
  .el-upload.el-upload--picture-card {
    float: left;
    width: 90px !important;
    height: 90px !important;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .el-upload-list__item.is-success {
    float: left;
    width: 90px !important;
    height: 90px !important;
  }
  .hide .el-upload--picture-card {
    display: none;
  }
  .el-upload-dragger {
    width: 90px !important;
    height: 90px !important;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
.img_upload_download_index {
  cursor: pointer;
  transition: 0.3s;
  position: absolute;
  top: 13px;
  font-size: 16px;
  border-radius: 50%;
  border: 1px solid #ccc;
  padding: 5px;
  &:hover {
    color: #496dff;
    border-color: #496dff;
  }
}
/* 取消 el-upload 列表中文件条目的动画 */
.el-upload-list__item {
  transition: none !important;
}
</style>
